home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / libiberty / concat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-12  |  2.9 KB  |  117 lines

  1. /* Concatenate variable number of strings.
  2.    Copyright (C) 1991 Free Software Foundation, Inc.
  3.    Written by Fred Fish @ Cygnus Support
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19.  
  20. /*
  21.  
  22. NAME
  23.  
  24.     concat -- concatenate a variable number of strings
  25.  
  26. SYNOPSIS
  27.  
  28.     #include <varargs.h>
  29.  
  30.     char *concat (s1, s2, s3, ..., NULL)
  31.  
  32. DESCRIPTION
  33.  
  34.     Concatenate a variable number of strings and return the result
  35.     in freshly malloc'd memory.
  36.  
  37.     Returns NULL if insufficient memory is available.  The argument
  38.     list is terminated by the first NULL pointer encountered.  Pointers
  39.     to empty strings are ignored.
  40.  
  41. NOTES
  42.  
  43.     This function uses xmalloc() which is expected to be a front end
  44.     function to malloc() that deals with low memory situations.  In
  45.     typical use, if malloc() returns NULL then xmalloc() diverts to an
  46.     error handler routine which never returns, and thus xmalloc will
  47.     never return a NULL pointer.  If the client application wishes to
  48.     deal with low memory situations itself, it should supply an xmalloc
  49.     that just directly invokes malloc and blindly returns whatever
  50.     malloc returns.
  51. */
  52.  
  53.  
  54. #include <varargs.h>
  55.  
  56. #define NULLP (char *)0
  57.  
  58. extern char *xmalloc ();
  59.  
  60. /* VARARGS */
  61. char *
  62. concat (va_alist)
  63.      va_dcl
  64. {
  65.   register int length = 0;
  66.   register char *newstr;
  67.   register char *end;
  68.   register char *arg;
  69.   va_list args;
  70.  
  71.   /* First compute the size of the result and get sufficient memory. */
  72.  
  73.   va_start (args);
  74.   while ((arg = va_arg (args, char *)) != NULLP)
  75.     {
  76.       length += strlen (arg);
  77.     }
  78.   newstr = (char *) xmalloc (length + 1);
  79.   va_end (args);
  80.  
  81.   /* Now copy the individual pieces to the result string. */
  82.  
  83.   if (newstr != NULLP)
  84.     {
  85.       va_start (args);
  86.       end = newstr;
  87.       while ((arg = va_arg (args, char *)) != NULLP)
  88.     {
  89.       while (*arg)
  90.         {
  91.           *end++ = *arg++;
  92.         }
  93.     }
  94.       *end = '\000';
  95.       va_end (args);
  96.     }
  97.  
  98.   return (newstr);
  99. }
  100.  
  101. #ifdef MAIN
  102.  
  103. /* Simple little test driver. */
  104.  
  105. main ()
  106. {
  107.   printf ("\"\" = \"%s\"\n", concat (NULLP));
  108.   printf ("\"a\" = \"%s\"\n", concat ("a", NULLP));
  109.   printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP));
  110.   printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP));
  111.   printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP));
  112.   printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP));
  113.   printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP));
  114. }
  115.  
  116. #endif
  117.